package ai.minimax.gui;

import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import ai.minimax.Config;
import ai.minimax.Controller;
import ai.minimax.entities.CellState;
import ai.minimax.entities.State;

public class Gui extends JFrame implements Observer, MouseListener{
	private static final long serialVersionUID = -390570070061130441L;

	private Controller controller;
	ImagePanel[][] ticTacToePanels = new ImagePanel[Config.DIMENSION][Config.DIMENSION];
	private boolean isPlayerTurn = !Config.MAX_STARTS;
	
	public Gui(Controller controller){
		super();
		this.controller = controller;
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(Config.DIMENSION*100,Config.DIMENSION*100);
		this.setVisible(true);
		this.setResizable(false);
		
		BufferedImage minImage;
		BufferedImage maxImage;
		try {
			minImage = ImageIO.read(new File(Config.CROSS_IMAGE_PATH));
			maxImage = ImageIO.read(new File(Config.CIRCLE_IMAGE_PATH));
			
			this.setLayout(new GridLayout(Config.DIMENSION,Config.DIMENSION));
			for(int i=0; i < Config.DIMENSION;i++){
				for(int j=0;j < Config.DIMENSION;j++){ 
					ticTacToePanels[i][j] = new ImagePanel(i, j, minImage, maxImage);
					ticTacToePanels[i][j].addMouseListener(this);
					this.add(ticTacToePanels[i][j]);
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	@Override
	public void update(Observable observable, Object argument){
		State currentState = (State) argument;
//		currentState.get
		CellState[][] field = currentState.getField();
		for(int i=0; i < field.length;i++){
			for(int j=0;j < field[i].length;j++){
				ticTacToePanels[i][j].setCurrentState(currentState.getField()[i][j]);
				this.repaint();
			}
		}
		int utility;
		if(currentState.terminalTest()){
			utility = currentState.getUtility();
			if(utility == 1){
				JOptionPane.showMessageDialog(this, "Verloren", "Ok um nochmal zu spielen", JOptionPane.OK_OPTION);
			}
			else if(utility == -1){
				JOptionPane.showMessageDialog(this, "Gewonnen!", "Ok um nochmal zu spielen", JOptionPane.OK_OPTION);
			}else{
				JOptionPane.showMessageDialog(this, "Unentschieden!", "Ok um nochmal zu spielen", JOptionPane.OK_OPTION);
			}
			controller.resetGame();
		}else{
			isPlayerTurn = !isPlayerTurn;	
		}
	}

	@Override
	public void mouseClicked(MouseEvent event) {
		if(isPlayerTurn){
			ImagePanel clickedPanel = (ImagePanel) event.getSource();
			if(clickedPanel.getCurrentState() == CellState.EMPTY){
				controller.doPlayerTurn(clickedPanel.getRow(), clickedPanel.getCol());
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent event) {}

	@Override
	public void mouseExited(MouseEvent event) {}

	@Override
	public void mousePressed(MouseEvent event) {}

	@Override
	public void mouseReleased(MouseEvent event) {}
}
